import 'package:flutter/material.dart';
import 'package:ytbusiness/config/style.dart';
import 'package:ytbusiness/config/yt_theme.dart';
import 'package:ytbusiness/page/repair_factory/repair_order/order_widget/image_info.dart';
import 'package:ytbusiness/page/repair_factory/repair_order/order_widget/labor_hour.dart';
import 'package:ytbusiness/page/repair_factory/repair_order/order_widget/parts_info.dart';
import 'package:ytbusiness/page/repair_factory/repair_order/order_widget/repair_order_info.dart';
import 'package:ytbusiness/utils/helper.dart';
import 'package:ytbusiness/utils/screen_adaptation.dart';
import 'package:ytbusiness/widgets/widgets.dart';
import 'order_widget/repair_car_info.dart';

class RepairRefunded extends StatefulWidget {
  //{title}
  final Map<String, dynamic> arguments;

  const RepairRefunded({Key key, this.arguments}) : super(key: key);

  @override
  _RepairRefundedState createState() => _RepairRefundedState();
}

class _RepairRefundedState extends State<RepairRefunded>
    with SingleTickerProviderStateMixin {
  TabController _tabController; //tab控制器
  List<String> titleList = ["订单信息", "车辆信息", "图片信息"];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _tabController = new TabController(length: titleList.length, vsync: this);

    _tabController.addListener(() {
      setState(() {});
    });
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: () {
        //输入框失焦
        FocusScope.of(context).requestFocus(FocusNode());
      },
      child: Scaffold(
        backgroundColor: YTStyle.bgColor,
        appBar: AppBar(
          elevation: 0.5,
          centerTitle: true,
          backgroundColor: Colors.white,
          title: Text(
            widget.arguments["title"] as String,
            style: TextStyle(
                color: Color(0xff323232),
                fontSize: setSp(18),
                fontWeight: FontWeight.w600),
            maxLines: 1,
            overflow: TextOverflow.ellipsis,
          ),
          bottom: TabBar(
            controller: _tabController,
            isScrollable: true,
            labelColor: YTTheme.theme,
            labelStyle:
                TextStyle(fontSize: setSp(17), fontWeight: FontWeight.w600),
            unselectedLabelColor: Color(0xff323232),
            unselectedLabelStyle:
                TextStyle(color: Colors.white, fontSize: setSp(17)),
            indicatorColor: YTTheme.theme,
            indicatorSize: TabBarIndicatorSize.label,
            tabs: titleList
                .asMap()
                .map((key, value) {
                  return MapEntry(key, _tabItem(titleList[key]));
                })
                .values
                .toList(),
          ),
          leading: GestureDetector(
            behavior: HitTestBehavior.opaque,
            onTap: () {
              Navigator.pop(context);
            },
            child: Row(
              children: [
                Container(
                    margin: EdgeInsets.only(left: setWidth(15)),
                    width: setWidth(10),
                    height: setWidth(17),
                    child: Image.asset(
                      "assets/images/return.png",
                      fit: BoxFit.cover,
                    )),
              ],
            ),
          ),
          actions: [
            Container(
              margin: EdgeInsets.only(right: setWidth(15)),
              width: setWidth(10),
              height: setWidth(17),
            ),
          ],
        ),
        body: CustomScrollView(
          slivers: [
            SliverToBoxAdapter(
              child: SizedBox(
                height: setWidth(15),
              ),
            ),
            //订单信息
            SliverToBoxAdapter(
              child: Container(
                padding: EdgeInsets.only(
                    left: setWidth(15),
                    right: setWidth(15),
                    bottom: setWidth(15)),
                color: Colors.white,
                child: _checkType(_tabController.index),
              ),
            ),

            SliverToBoxAdapter(
              child: SizedBox(
                height: setWidth(15),
              ),
            ),
            //配件信息
            SliverToBoxAdapter(
              child: PartsInfo(),
            ),
            SliverToBoxAdapter(
              child: SizedBox(
                height: setWidth(15),
              ),
            ),
            //工时信息
            SliverToBoxAdapter(
              child: LaborHour(),
            ),
            //结算信息
            SliverToBoxAdapter(
              child: Container(
                  margin: EdgeInsets.only(top: setWidth(15)),
                  child: autoMakersSettlement(
                      price: "600", serve: "600", sum: "1460.00")),
            ),
            //开票信息
            SliverToBoxAdapter(
              child: Container(
                color: Colors.white,
                margin: EdgeInsets.only(top: setWidth(15)),
                padding: EdgeInsets.only(
                    left: setWidth(15), top: setWidth(18), right: setWidth(15)),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.start,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Container(
                      child: Text(
                        "开票信息",
                        style: TextStyle(
                          color: Color(0xff323232),
                          fontSize: setSp(16),
                        ),
                      ),
                    ),
                    SizedBox(
                      height: setWidth(22),
                    ),
                    //开票信息
                    _backInvoice(),
                    //确认开票
                    GestureDetector(
                      behavior: HitTestBehavior.opaque,
                      onTap: () {},
                      child: Container(
                        height: setWidth(44),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Text(
                              "已重新开票并提交",
                              style: TextStyle(
                                  color: Colors.white,
                                  fontSize: setSp(18),
                                  fontWeight: FontWeight.w600),
                            ),
                          ],
                        ),
                        decoration: BoxDecoration(
                            color: Color(0xff00C65F),
                            borderRadius: BorderRadius.circular(2)),
                      ),
                    ),
                    SizedBox(
                      height: setWidth(46),
                    ),
                  ],
                ),
              ),
            ),
            SliverToBoxAdapter(
              child: SizedBox(
                height: setWidth(22),
              ),
            )
          ],
        ),
      ),
    );
  }

  Widget _tabItem(String title) {
    return Tab(text: title);
  }

  //切换view
  // ignore: missing_return
  Widget _checkType(int type) {
    switch (type) {
      case 0:
        return RepairOrderInfo(
          orderState: widget.arguments["title"] as String,
        );
        break;
      case 1:
        return RepairCarInfo();
        break;
      case 2:
        return ImageListInfo();
        break;
    }
  }

  //回票信息
  Widget _backInvoice() {
    return Column(
      children: [
        invoiceInfo("开票金额:", "34792.00（含税）", richText: "工时费"),
        invoiceInfo("发票抬头:", "太平洋保险股份有限公司安徽合肥分公司"),
        invoiceInfo("税号:", "983798oqiuwqu2323"),
        copyInfo("地址:", "安徽省合肥市"),
        copyInfo("邮寄地址:", "某某某 12545689756 安徽省合肥市庐阳区万科中心2154"),
        SizedBox(
          height: setWidth(50),
        ),
        Container(
          margin: EdgeInsets.only(bottom: setWidth(34)),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              //快递方式
              Container(
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.start,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Container(
                      child: Text("快递方式:"),
                    ),
                    SizedBox(
                      width: setWidth(9),
                    ),
                    Expanded(
                        child: Container(
                      alignment: Alignment.center,
                      height: setWidth(30),
                      child: TextField(
                        textAlign: TextAlign.start,
                        maxLines: 1,
                        keyboardType: TextInputType.multiline,
                        autofocus: false,
                        decoration: InputDecoration(
                          enabledBorder: OutlineInputBorder(
                            borderRadius: BorderRadius.circular(2.0),
                            borderSide: BorderSide(
                                color: Color(0xff989898), width: setWidth(0.5)),
                          ),
                          focusedBorder: OutlineInputBorder(
                            borderRadius: BorderRadius.circular(2.0),
                            borderSide: BorderSide(
                                color: Color(0xff989898), width: setWidth(0.5)),
                          ),
                          contentPadding: EdgeInsets.only(
                              top: setWidth(3), left: setWidth(12)),
                          hintText: "请填写快递方式",
                          hintStyle: TextStyle(
                              color: Color(0xffC6C9CE), fontSize: setSp(16)),
                        ),
                      ),
                    )),
                  ],
                ),
              ),
              SizedBox(
                height: setWidth(15),
              ),
              //快递单号
              Container(
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.start,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Container(
                      child: Text("快递单号:"),
                    ),
                    SizedBox(
                      width: setWidth(9),
                    ),
                    Expanded(
                        child: Container(
                      alignment: Alignment.center,
                      height: setWidth(30),
                      child: TextField(
                        textAlign: TextAlign.start,
                        maxLines: 1,
                        keyboardType: TextInputType.multiline,
                        autofocus: false,
                        decoration: InputDecoration(
                          enabledBorder: OutlineInputBorder(
                            borderRadius: BorderRadius.circular(2.0),
                            borderSide: BorderSide(
                                color: Color(0xff989898), width: setWidth(0.5)),
                          ),
                          focusedBorder: OutlineInputBorder(
                            borderRadius: BorderRadius.circular(2.0),
                            borderSide: BorderSide(
                                color: Color(0xff989898), width: setWidth(0.5)),
                          ),
                          contentPadding: EdgeInsets.only(
                              top: setWidth(3), left: setWidth(12)),
                          hintText: "请填写快递单号",
                          hintStyle: TextStyle(
                              color: Color(0xffC6C9CE), fontSize: setSp(16)),
                        ),
                      ),
                    )),
                  ],
                ),
                height: setWidth(40),
              ),
            ],
          ),
        ),
      ],
    );
  }

  //发票信息
  Widget invoiceInfo(String title, String content, {String richText = ""}) {
    return Container(
      margin: EdgeInsets.only(bottom: setWidth(18)),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            width: setWidth(64),
            child: Text(
              title,
              style: TextStyle(color: Color(0xff323232), fontSize: setSp(14)),
            ),
          ),
          Expanded(
            child: Row(
              children: [
                Container(
                  child: Text(content,
                      style: TextStyle(
                          color: Color(0xff323232), fontSize: setSp(14))),
                ),
                Container(
                  child: Text(richText,
                      style: TextStyle(
                          color: Color(0xffFF6701), fontSize: setSp(14))),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  //拷貝发票信息
  Widget copyInfo(String title, String content) {
    return Container(
      margin: EdgeInsets.only(bottom: setWidth(18)),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            width: setWidth(70),
            child: Text(
              title,
              style: TextStyle(color: Color(0xff323232), fontSize: setSp(15)),
            ),
          ),
          Expanded(
            child: Container(
              child: Text(content,
                  style:
                      TextStyle(color: Color(0xff323232), fontSize: setSp(15))),
            ),
          ),
          SizedBox(
            width: setWidth(31),
          ),
          GestureDetector(
            onTap: () {
              copy(content);
            },
            child: Container(
              child: Text(
                "复制",
                style: TextStyle(color: Color(0xff1666F1), fontSize: setSp(15)),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
